﻿<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>添加文档</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
  <style type="text/css">
   .layui-select-title input{
       width:300px;
   }
  </style>
</head>
<body>
  <div class="layui-row" style="padding: 20px 30px 0 0;">
   <div class="layui-col-md12" id="uploadfile">
    <div class="layui-form-item" >
        <label class="layui-form-label">上传文件：</label>
        <div class="layui-input-inline">
        <div class="layui-upload">
            <button type="button" class="layui-btn" id="btn_upload_change"><i class="layui-icon"></i>选择文件</button>
        </div>
        </div>
    </div>
   </div>
   <div class="layui-form" lay-filter="form-add" id="form-add" style="display:none;">
    <input type="hidden" name="docfile" value="" />
    <input type="hidden" name="filetype" value="" />
    <input type="hidden" name="action" value="add" />
    <div class="layui-form-item">
      <label class="layui-form-label" style="width: 100px">文档标题</label>
      <div class="layui-input-inline">
        <input type="text" name="title" lay-verify="required" style="width:300px;" value="" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label" style="width: 100px">文档文号</label>
      <div class="layui-input-inline">
        <input type="text" name="number" style="width:300px;" value="" class="layui-input" lay-verify="required">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label" style="width: 100px">发布年份</label>
      <div class="layui-input-inline">
        <input type="text" name="yeartype_name" style="width:300px;" value="" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label" style="width: 100px">文档类型</label>
      <div class="layui-input-inline">
        <select name="doctype" id="doctype">
          <option value="">请选择类型</option>
        </select>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label" style="width: 100px">发布机关</label>
      <div class="layui-input-inline">
        <select name="orgtype" id="orgtype">
          <option value="">请选择机关</option>
        </select>
      </div>
    </div>
    <div class="layui-form-item layui-hide">
      <input type="button" lay-submit lay-filter="btn_submit" id="btn_submit" value="确定">
    </div>
    </div>
  </div>
  <script src="/plugins/layui/layui.js"></script>
  <script>
  layui.config({
    base: '/plugins/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'form','upload'], function(){
    var $ = layui.$, form = layui.form, router = layui.router(), upload = layui.upload;
    var load_start = layer.load(2);
    upload.render({
      elem: '#btn_upload_change'
      ,url: 'doc_add?action=upload'
      ,accept: 'file'
      , exts: 'doc|docx|xls|xlsx|ppt|pptx|txt|pdf'
      ,choose: function(){
        layer.load();
      }
      , done: function (res) {
          layer.closeAll('loading'); 
          if (res.code == 0) {
              form.val("form-add", res.data);
              $("#uploadfile").hide();
              $("#form-add").show();
          }
          else {
              layer.alert(res.msg);
          }
      }
    });
    $.ajax({
          type: "get",
          url: "/type/get_typelist",
          data: {markup:'doctype'},
          dataType: "json",
          success: function (res) {
            if (res.code == 0) {
                $.each(res.typelist, function (i, v) {
                    $("#doctype").append("<option value='" + v.id + "'>" + v.title + "</option>");
                });
                layui.form.render("select");
            }
            else {
                layer.close(load_start);
                layer.alert(res.msg);
            }
        }
    });
    $.ajax({
          type: "get",
          url: "/type/get_typelist",
          data: {markup:'orgtype'},
          dataType: "json",
          success: function (res) {
            layer.close(load_start);
            if (res.code == 0) {
                $.each(res.typelist, function (i, v) {
                    $("#orgtype").append("<option value='" + v.id + "'>" + v.title + "</option>");
                });
                layui.form.render("select");
            }
            else {
                layer.alert(res.msg);
            }
        }
    });
    //监听提交
    form.on('submit(btn_submit)', function (data) {
      var load_index = layer.load(2);
        var field = data.field; //获取提交的字段
      var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引  
      //提交 Ajax 成功后，关闭当前弹层并重载表格
      $.ajax({
            type: "post",
            url: "doc_add",
            data: field,
            dataType: "json",
            success: function (res) {
              if (res.code == 0)
              {
                layer.close(load_index);
                layer.msg(res.msg,{time:1*1000},function(){
                  parent.layui.table.reload('dds-list'); //重载表格
                  parent.layer.close(index); //再执行关闭 
                });
              }
              else
              {
                layer.close(load_index);
                layer.alert(res.msg);
              }
            }
      });
    });
  })
  </script>
</body>
</html>